/**
 * 线路产品管理模块 API 接口
 * @description 线路产品管理相关的所有API接口封装
 * @author CRM开发团队
 * @version 1.0.0
 */

import { get, post, put, del as deleteRequest, patch } from '@/utils/request'

// ==========================================
// 类型定义
// ==========================================

// 产品状态枚举
export type ProductStatus = '正常销售' | '暂停销售' | '即将上线' | '已下架' | '季节性暂停'

// 库存状态枚举
export type InventoryStatus = '充足' | '紧张' | '不足' | '售罄'

// 产品分类
export interface ProductCategory {
  primary: '国内游' | '出境游' | '周边游' | '定制游'
  secondary: string
  tertiary?: string
}

// 价格结构
export interface PriceStructure {
  adult: number           // 成人价格
  child?: number          // 儿童价格
  infant?: number         // 婴儿价格
  senior?: number         // 老人价格
  member?: number         // 会员价格
}

// 库存配置
export interface InventoryConfig {
  totalStock: number      // 总库存
  availableStock: number  // 可售库存
  soldCount: number       // 已售数量
  reservedStock: number   // 预留库存
  minGroup: number        // 最少成团人数
}

// 产品基础信息
export interface Product {
  id: string
  productCode: string
  name: string
  subtitle?: string
  category: ProductCategory
  destination: string[]
  duration: number
  description: string
  highlights: string[]
  
  // 价格信息
  pricing: PriceStructure
  
  // 库存信息
  inventory: InventoryConfig
  
  // 状态信息
  status: ProductStatus
  inventoryStatus: InventoryStatus
  
  // 销售数据
  salesData: {
    monthSales: number
    totalSales: number
    avgRating: number
  }
  
  // 时间信息
  createdAt: string
  updatedAt: string
  createdBy: string
  updatedBy: string
  
  // 产品图片
  mainImage?: string
  images?: string[]
  
  // 产品标签
  tags: string[]
}

// 产品列表查询参数
export interface ProductListQuery {
  page?: number
  pageSize?: number
  keyword?: string
  status?: ProductStatus
  category?: string
  inventoryStatus?: InventoryStatus
  priceMin?: number
  priceMax?: number
  tags?: string[]
  createdAtStart?: string
  createdAtEnd?: string
  sortBy?: string
  sortOrder?: 'asc' | 'desc'
}

// 产品统计数据
export interface ProductStatistics {
  totalProducts: number
  onSaleProducts: number
  inventoryWarnings: number
  monthSales: number
  avgPrice: number
  priceGrowth: number
  salesGrowth: number
}

// 批量操作参数
export interface BatchOperationParams {
  ids: string[]
  operation: 'updateStatus' | 'updatePrice' | 'updateCategory' | 'delete'
  data?: any
}

// API响应类型
export interface ApiResponse<T = any> {
  code: number
  message: string
  data: T
  success: boolean
}

export interface ListResponse<T = any> {
  items: T[]
  total: number
  page: number
  pageSize: number
}

// ==========================================
// API 接口函数
// ==========================================

/**
 * 获取产品列表
 */
export const getProductList = (params?: ProductListQuery): Promise<ApiResponse<ListResponse<Product>>> => {
  return get('/api/products/list', params)
}

/**
 * 获取产品统计数据
 */
export const getProductStatistics = (): Promise<ApiResponse<ProductStatistics>> => {
  return get('/api/products/statistics')
}

/**
 * 获取产品详情
 */
export const getProductDetail = (id: string): Promise<ApiResponse<Product>> => {
  return get(`/api/products/${id}`)
}

/**
 * 创建产品
 */
export const createProduct = (data: Partial<Product>): Promise<ApiResponse<Product>> => {
  return post('/api/products', data)
}

/**
 * 更新产品
 */
export const updateProduct = (id: string, data: Partial<Product>): Promise<ApiResponse<Product>> => {
  return put(`/api/products/${id}`, data)
}

/**
 * 删除产品
 */
export const deleteProduct = (id: string): Promise<ApiResponse<boolean>> => {
  return deleteRequest(`/api/products/${id}`)
}

/**
 * 批量操作产品
 */
export const batchOperateProducts = (params: BatchOperationParams): Promise<ApiResponse<any>> => {
  return post('/api/products/batch-operation', params)
}

/**
 * 产品搜索建议
 */
export const getSearchSuggestions = (keyword: string): Promise<ApiResponse<string[]>> => {
  return get('/api/products/search-suggestions', { keyword })
}

/**
 * 获取产品分类选项
 */
export const getProductCategories = (): Promise<ApiResponse<any>> => {
  return get('/api/products/categories')
}

/**
 * 获取产品标签选项
 */
export const getProductTags = (): Promise<ApiResponse<string[]>> => {
  return get('/api/products/tags')
}

/**
 * 复制产品
 */
export const copyProduct = (id: string, name: string): Promise<ApiResponse<Product>> => {
  return post(`/api/products/${id}/copy`, { name })
}

/**
 * 更新产品状态
 */
export const updateProductStatus = (id: string, status: ProductStatus): Promise<ApiResponse<Product>> => {
  return patch(`/api/products/${id}/status`, { status })
}

/**
 * 获取价格历史
 */
export const getPriceHistory = (id: string): Promise<ApiResponse<any[]>> => {
  return get(`/api/products/${id}/price-history`)
}

/**
 * 获取销售分析数据
 */
export const getSalesAnalytics = (id: string, timeRange?: string): Promise<ApiResponse<any>> => {
  return get(`/api/products/${id}/sales-analytics`, { timeRange })
}